package com.itheima.dao;

import com.itheima.pojo.Blog;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import tk.mybatis.mapper.common.Mapper;

import java.util.Date;
import java.util.List;
import java.util.Map;

public interface BlogMapper extends Mapper<Blog> {

    /**
     * 获取一年内每天的文章贡献度
     *
     * @param startTime
     * @param endTime
     * @return
     */
    @Select("select date(b.create_time) date,ifnull(count(uid),0) count from t_blog b where b.create_time between #{startTime} and #{endTime} group by date order by date ")
    @Results({
            @Result(column = "count", property = "count"),
            @Result(column = "date", property = "date")
    })
    List<Map> getBlogContributeCount(@Param("startTime") Date startTime, @Param("endTime") Date endTime);


    @Select("SELECT b.tag_uid tagUid,t.content name,count(b.uid) value FROM t_blog b LEFT JOIN t_tag t ON t.uid = b.tag_uid where b.tag_uid is not null and t.content is not null GROUP BY b.tag_uid")
    @Results({
            @Result(column = "name", property = "name"),
            @Result(column = "value", property = "value"),
            @Result(column = "tagUid", property = "tagUid"),
    })
    List<Map> getBlogCountByTag();

    /**
     * 获取每天的文章贡献度
     */
    @Select("SELECT count(uid) FROM t_blog where create_time BETWEEN #{startTime} and #{endTime} ")
    Integer getBlogCount4Day(@Param("startTime") String startTime, @Param("endTime") String endTime);


    // //获取博客类型
    @Select("select blog_sort_uid from t_blog where uid=#{blogUid}")
    String getBlogTypeByBlogUid(@Param("blogUid") String blogUid);

    //根据博客类型查询同类的博客
    @Select("select * from t_blog where blog_sort_uid = #{sortId}")
    @Results({@Result(column = "tag_uid", property = "tagUid"),
            @Result(column = "click_count", property = "clickCount"),
            @Result(column = "collect_count", property = "collectCount"),
            @Result(column = "file_uid", property = "fileUid"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "update_time", property = "updateTime"),
            @Result(column = "admin_uid", property = "adminUid"),
            @Result(column = "is_original", property = "isOriginal"),
            @Result(column = "articles_part", property = "articlesPart"),
            @Result(column = "is_publish", property = "isPublish"),
            @Result(column = "blog_sort_uid", property = "blogSortUid"),
            @Result(column = "start_comment", property = "startComment")
    })
    List<Blog> getSameBlogByBlogUid(@Param("sortId") String sortId);


    //根据标签查询相关博客
    @Select("select * from t_blog where tag_uid=#{tagUid}")
    @Results({@Result(column = "tag_uid", property = "tagUid"),
            @Result(column = "click_count", property = "clickCount"),
            @Result(column = "collect_count", property = "collectCount"),
            @Result(column = "file_uid", property = "fileUid"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "update_time", property = "updateTime"),
            @Result(column = "admin_uid", property = "adminUid"),
            @Result(column = "is_original", property = "isOriginal"),
            @Result(column = "articles_part", property = "articlesPart"),
            @Result(column = "is_publish", property = "isPublish"),
            @Result(column = "blog_sort_uid", property = "blogSortUid"),
            @Result(column = "start_comment", property = "startComment")
    })
    List<Blog> getSameBlogByTagUid(String tagUid);


    @Select("update  t_comment set TYPE ='1' where blog_uid=#{uid}")
    void praiseBlogByUid(@Param("uid") String uid);

    @Select("SELECT content FROM `t_blog` where create_time like '%${createMonth}%' OR update_time LIKE '%${updateMonth}%'")
    List<String> getArticleByMonth(@Param("createMonth") String createTime, @Param("updateMonth") String updateTime);

    @Select("SELECT DATE_FORMAT(create_time,'%Y%m') months FROM t_blog GROUP BY months ")
    List<String> getSortList();

    /**
     * 根据tagUid获取blogs
     * @param tagUid
     * @return
     */
    @Select("SELECT * from t_blog WHERE tag_uid = #{tagUid}")
    List<Blog> getBlogsByTagUid(@Param("tagUid") String tagUid);
}
